/*******************************************************************************
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 *
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal;

import net.sourceforge.perforce.ui.IPerforceUIConstants;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.dialogs.ConfirmationDialog;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.team.FileModificationValidationContext;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * @version $Revision: 493 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class OpenValidator extends AbstractOpenValidator {
  // -------------------------------------------------------------------------
  // Public Constructors

  /** Constructor for OpenValidator. */
  public OpenValidator() {
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  public IStatus validateEdit(IFile[] files, FileModificationValidationContext context) {
    IPreferenceStore store = PerforceUIPlugin.getPlugin().getPreferenceStore();
    if (!store.getBoolean(IPerforceUIConstants.PREF_DONT_PROMPT_AUTOMATIC_OPEN)) {
      files = filesToCheckout(files);
      if (files.length > 0) {
        if (!promptForCheckout(context, files)) {
          return new Status(IStatus.ERROR, PerforceUIPlugin.ID, IStatus.ERROR,
              "The user refused to open the file for edit.", null);
        }
      }
    }

    return new Status(IStatus.OK, PerforceUIPlugin.ID, IStatus.OK, "", null);
  }

  //----------------------------------------------------------------------------
  // - Private Methods

  /**
   * Prompts the user for checkout of the given files.
   *
   * @param context the shell.
   * @param files the files to open.
   * @return true if files should be opened for edit.
   */
  private boolean promptForCheckout(FileModificationValidationContext context, final IFile[] files) {
    final String message =
        "The system has indicated that one or more files not currently "
            + "opened for edit need to be changed. Do you want them to be " + "opened for edit?";

    final boolean[] pressedCancel = {true};

    Display display = Display.getCurrent();
    if (display == null) {
      display = Display.getDefault();
    }

    final Shell shell = (Shell) context.getShell();
    display.syncExec(new Runnable() {
      public void run() {
        pressedCancel[0] =
            !ConfirmationDialog.openConfirmation(shell, "Open for Edit", message, files);
      }
    });

    return !pressedCancel[0];
  }
}
